<?php
class BdDtsController extends AppController {

	var $name = 'BdDts';
	var $helpers = array('Html', 'Form');

	function beforeFilter(){
        parent::beforeFilter();
        $curUser = $this->Auth->user();
        if($curUser['User']['ROLE']!='admin'){
            $this->Session->setFlash('Bạn phải đăng nhập bằng quyền ADMIN để sử dụng chức năng này');
            $this->redirect('/controls');
        }
    }



	function index() {
		$this->BdDt->recursive = 0;
		$this->set('bdDts', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid BdDt.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('bdDt', $this->BdDt->read(null, $id));
	}



	function add() {
		if (!empty($this->data)) {
			$this->BdDt->create();
			
			$pbd_id = $this->data['BdDt']['bayden_id'];
			foreach($this->data['BdDt']['doituong_id'] as $key=>$value){
				$record['Bayden']['id'] = $pbd_id;
				$record['Doituong']['id'] = $value;
				if (!$this->BdDt->saveAll($record)) {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại.', true));
				
			} 
			
			
			}
				$this->Session->setFlash(__('Thông tin bạn nhập đã được lưu', true));
				$this->redirect(array('action'=>'index'));
		}
		
		//$trambvtvs = $this->Canbo->Trambvtv->find('list',array('fields'=>array('Trambvtv.id','Ten_tr')));
		$baydens = $this->BdDt->Bayden->find('list',array('fields'=>array('Bayden.id','TEN_BD')));
		$doituongs = $this->BdDt->Doituong->find('list',array('fields'=>array('Doituong.id','TEN_DT')));
		$this->set(compact('baydens', 'doituongs'));
	}
	

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid BdDt', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->BdDt->save($this->data)) {
				$this->Session->setFlash(__('Thông tin bạn nhập đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->BdDt->read(null, $id);
		}
		/*
		$baydens = $this->BdDt->Bayden->find('list');
		$doituongs = $this->BdDt->Doituong->find('list');
		*/
		$baydens = $this->BdDt->Bayden->find('list',array('fields'=>array('Bayden.id','TEN_BD')));
		$doituongs = $this->BdDt->Doituong->find('list',array('fields'=>array('Doituong.id','TEN_DT')));
		$this->set(compact('baydens','doituongs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for BdDt', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->BdDt->del($id)) {
			$this->Session->setFlash(__('Thao tác XÓA đã hoàn tất', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->BdDt->recursive = 0;
		$this->set('bdDts', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid BdDt.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('bdDt', $this->BdDt->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->BdDt->create();
			if ($this->BdDt->save($this->data)) {
				$this->Session->setFlash(__('The BdDt has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The BdDt could not be saved. Please, try again.', true));
			}
		}
		$baydens = $this->BdDt->Bayden->find('list');
		$doituongs = $this->BdDt->Doituong->find('list');
		$this->set(compact('baydens', 'doituongs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid BdDt', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->BdDt->save($this->data)) {
				$this->Session->setFlash(__('The BdDt has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The BdDt could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->BdDt->read(null, $id);
		}
		$baydens = $this->BdDt->Bayden->find('list');
		$doituongs = $this->BdDt->Doituong->find('list');
		$this->set(compact('baydens','doituongs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for BdDt', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->BdDt->del($id)) {
			$this->Session->setFlash(__('BdDt deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>